package messageMeTransfer.utilities;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;

import messageMeTransfer.interfaces.IConnectionListener;

public class ClientConnectionReciever extends Thread {
	
	private ServerSocket socket = null;
	private List<IConnectionListener> listeners = new ArrayList<IConnectionListener>();

	public ClientConnectionReciever(ServerSocket socket) {
		this.socket = socket;
	}

	public synchronized void addListener(IConnectionListener listener) {
		this.listeners.add(listener);
	}

	public void run() {

		try {
			Client client;
			while ((client = new Client(this.socket.accept())) != null) {
				this.addClient(client);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public synchronized void addClient(Client client) {
		for (IConnectionListener listener : this.listeners) {
			listener.connectionRecieved(client);
		}
	}
}
